//
//  UIImage+XLClickIcon.m
//  07-练习
//
//  Created by sum on 15/5/17.
//  Copyright (c) 2015年 sum123. All rights reserved.
//

#import "UIImage+XLClickIcon.h"

@implementation UIImage (XLClickIcon)

+ (instancetype)clickIconWithImageName:(NSString *)imageName clickMargin:(CGFloat)clickMargin clickMarginColor:(UIColor *)clickMarginColor
{
    return [self clickIconWithImage:[UIImage imageNamed:imageName] clickMargin:clickMargin clickMarginColor:clickMarginColor];
}


+ (instancetype)clickIconWithImage:(UIImage *)image clickMargin:(CGFloat)clickMargin clickMarginColor:(UIColor *)clickMarginColor
{
    // 开启上下文
    UIGraphicsBeginImageContext(image.size);
    
    // 画
    CGFloat centerX = image.size.width * 0.5;
    CGFloat centerY = image.size.height * 0.5;
    CGFloat radius = MIN(centerX, centerY);
    
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(centerX, centerY) radius:radius startAngle:0 endAngle:M_PI * 2 clockwise:YES];
    [clickMarginColor setFill];
    [path fill];
    
    path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(centerX, centerY) radius:radius - clickMargin startAngle:0 endAngle:M_PI * 2 clockwise:YES];
    // 裁剪
    [path addClip];
    
    [image drawInRect:CGRectMake(centerX - radius, centerY - radius, radius * 2, radius * 2)];
    
    // 取得上下文中得图片
    image = UIGraphicsGetImageFromCurrentImageContext();
    
    // 关闭上下文
    UIGraphicsEndImageContext();
    
    // 返回裁剪好的图片
    return image;

}

@end
